package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CheckOutListAdapter
import com.c.ctgproject.adapter.guang.MaterialOutAboutOrderAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityMaterialOutAboutOrderBinding
import com.c.ctgproject.entity.guang.MaterialOutAboutOrderEntity
import com.c.ctgproject.entity.guang.OutCheckEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerPosition

/**
 *作者:LiBW
 *创建日期: 2021/10/18
 *描述:新建放行申请 关联出库单 页面
 */
class MaterialOutAboutOrderActivity : BaseActivityV2(), MaterialOutAboutOrderAdapter.MaterialOutAboutOrderAdapterListener {
    private val mBinding by lazy { ActivityMaterialOutAboutOrderBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private val mList = ArrayList<MaterialOutAboutOrderEntity.DataBean.ListBean>()
    private val mAdapter by lazy { MaterialOutAboutOrderAdapter(this, mList, this) }

    private var search = ""
    override fun onResume() {
        super.onResume()
        page = 1
        getData()
    }

    override fun event() {
        mBinding.mRecyclerview.adapter = mAdapter
        mBinding.mRecyclerview.layoutManager = LinearLayoutManager(this)

    }

    override fun click() {

    }

    private fun getData() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.listCorrelationItem(search, page), MyObserverHandlerLoading(this, loading) {
            MyLog.d("回调:$it")
            val data = getGson().fromJson(it, MaterialOutAboutOrderEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mRecyclerview.handlerPosition(page)

            }
        })
    }

    override fun onBtnCheck(position: Int) {
        val intent = Intent()
        intent.putExtra(MyConstants.ORDER_ID, mList[position].outOrderId)
        intent.putExtra(MyConstants.ORDER_NO, mList[position].bizNo)
        setResult(1001, intent)
        finish()
    }
}